/*
 * ============================================================================
 * Licensed Materials - Property of IBM
 * Project  Zero
 *
 * (C) Copyright IBM Corp. 2007  All Rights Reserved.
 *
 * US Government Users Restricted Rights - Use, duplication or disclosure
 * restricted by GSA ADP Schedule Contract with IBM Corp.
 * ============================================================================
 * Copyright (c) 1999 - 2006 The PHP Group. All rights reserved.
 * ============================================================================
 */
package com.ibm.p8.engine.parser.core;

//////////////////////////////////////////////////////////////////
//                                                              //
//   This file was auto-generated by the LPG Eclipse Tooling.   //
//   It is safe to edit this file. It will not be overwritten.  //
//                                                              //
//////////////////////////////////////////////////////////////////
					
					
import java.util.ArrayList;

import org.jikes.lpg.runtime.TokenStream;


//
// LexStream holds an arraylist of tokens scanned 
// from the input stream by Scanner.java
//
public class LexStream implements TokenStream {
	public int index = 0;
	public int len = 0;
	public CharStream charStream;
	public ArrayList<Token> tokens;

	public LexStream(CharStream charStream) {
		this.charStream = charStream;
		tokens = new ArrayList<Token>(5000);
		addBadToken();
	}

	public void addToken(Token token) {
		tokens.add(token);
	}

	public void addBadToken() {
		tokens.add(new Token());
	}

	public String getTokenText(int tokenNumber) {
		Token t = tokens.get(tokenNumber);
		return t.toString(charStream.getContents());
	}

	public int getStartOffset(int tokenNumber) {
		Token t = tokens.get(tokenNumber);
		return t.getStartOffset();
	}

	public int getTokenLength(int tokenNumber) {
		Token t = tokens.get(tokenNumber);
		return t.getEndOffset() - t.getStartOffset() + 1;
	}

	public int getLineNumberOfTokenAt(int tokenNumber) {
		Token t = tokens.get(tokenNumber);
		return charStream.getLineNumberOfCharAt(t.getEndOffset());
	}

	public int getColumnOfTokenAt(int tokenNumber) {
		Token t = tokens.get(tokenNumber);
		return charStream.getColumnOfCharAt(t.getStartOffset());
	}

	public Token getTokenAt(int tokenNumber) {
		return tokens.get(tokenNumber);
	}

	public void dumpToken(int tokenNumber) {
		System.out.print(" (" + getKind(tokenNumber) + ")\t : ");
		System.out.print(getStartOffset(tokenNumber));
		System.out.print(" \t " + getLineNumberOfTokenAt(tokenNumber));
		System.out.print(" \t " + getColumnOfTokenAt(tokenNumber));
		System.out.print(" \t " + getTokenLength(tokenNumber));
		System.out.print("    \t\"" + getTokenText(tokenNumber)+"\"");
		System.out.println();
	}

	public int next(int tokenNumber) {
		return (++tokenNumber < len ? tokenNumber : len - 1);
	}

	// Methods that implement the TokenStream Interface
	//
	//
	public int getToken() {
		index = next(index);
		if (false && Parser.getDebug()) {
			Token token = getTokenAt(index);
			String kind = token.getTokenKindName();
			if (kind == null) {
				System.out.println("Shift: \"" + token + "\"");
			} else {
				System.out.println("Shift: " + kind + " \"" +
					token.toString(charStream.getContents()) + "\"");
			}	
		}	
		return index;
	}
	
	public int getKind(int tokenNumber) {
		Token t = tokens.get(tokenNumber);
		return t.getKind();
	}

	public int getNext(int tokenNumber) {
		return (++tokenNumber < len ? tokenNumber : len - 1);
	}

	public int peek() {
		return next(index);
	}

	public void reset() {
		index = 0;
	}

	public int badToken() {
		return 0;
	}

	public void reportError(int tokenNumber, String code) {
		if (code.equalsIgnoreCase("BadParseSymFileException")) {
			System.out.println("****Parse Symbol File is not valid for parsing ");
		} else {
			System.out.println("****" + code + " " + tokenNumber);
			dumpToken(tokenNumber);
		}
	}

	public void addEofToken(int eofKind) {
		Token token = new Token();
		token.setStartOffset(charStream.getIndex());
		token.setEndOffset(charStream.getIndex());
		token.setKind(eofKind);
		token.setLine(charStream.getLine());
		addToken(token);
		len = tokens.size();
	}

	public boolean isValidForParser() {
		return charStream.isValidForParser();
	};
	
}

